using System.Xml.Linq;
using TDUnit.Common.TestDataParser;

namespace TDUnit.TestDataParsers.XmlParser
{
    public abstract class ElementProcessorBase
    {
        protected static void AssertRootElementIsValid(XDocument document)
        {
            if (document.Root == null || document.Root.Name.LocalName != "TestData")
                throw new TestDataParserException("Dataset file must contain the root element <TestData>.");
        }

        protected static bool ParseAttributeAsBoolean(XAttribute attribute, string attributeName)
        {
            if (attribute == null)
                return false;
            bool autoIdent;

            if (attribute.Value.Equals("1")) return true;
            if (attribute.Value.Equals("0")) return false;

            if (!bool.TryParse(attribute.Value, out autoIdent))
                throw new XmlTestDataParserException(
                    string.Format("Could not parse value '{0}'. {1} should be set to 'True' or 'False' if specified.", attribute.Value, attributeName), attribute);

            return autoIdent;
        }
    }
}